﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.Threading;

namespace Directory.Web
{
    /// <summary>
    /// CustomAuthenticateModule 的摘要说明
    /// </summary>
    public sealed class CustomAuthenticateModule : IHttpModule
    {
        public String ModuleName
        {
            get { return "CustomAuthenticateModule"; }
        }

        #region IHttpModule 成员

        void IHttpModule.Dispose()
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IHttpModule.Init(HttpApplication context)
        {
            //throw new Exception("The method or operation is not implemented.");

			context.PostAuthenticateRequest += new EventHandler(context_PostAuthenticateRequest);
        }

		void context_PostAuthenticateRequest(object sender, EventArgs e)
		{
			IPrincipal user = HttpContext.Current.User;

			if (user.Identity.IsAuthenticated && user.Identity.AuthenticationType == "Forms")
			{
				FormsIdentity formIdentity = user.Identity as FormsIdentity;

				CustomIdentity identity = new CustomIdentity(formIdentity.Ticket);

				CustomPrincipal principal = new CustomPrincipal(identity);
				HttpContext.Current.User = principal;

				Thread.CurrentPrincipal = principal;
			}
		}

        #endregion
    }
}
